package com.zw.pdm.module.designtask.controller;
import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.basic.Dept;
import com.zw.pdm.entity.designtask.DsnGant;
import com.zw.pdm.module.designtask.mapper.DsnGantMapper;
import com.zw.pdm.module.designtask.model.DsnGantBillModel;
import com.zw.pdm.module.designtask.model.DsnGantWorkModel;
import com.zw.pdm.module.designtask.service.DsnGantService;
import com.zw.pdm.module.designtask.service.DsnProcessService;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.report.service.SheetService;
import com.zw.pdm.module.system.mapper.DeptMapper;
import com.zw.pdm.util.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 任务项编辑/查询
 * <p>
 * 权限：
 * dsn:proTasks:view 任务项编辑查看
 * dsn:proTasks:edit 编辑、保存
 * dsn:proTaskinfor:view 任务项查询
 * @Author: China.wsg
 * @Date: 2020/10/23 08:58
 */
@Controller
@RequestMapping("/designTask/gant")
@SuppressWarnings("all")
public class DsnGantController extends BaseController {

    @Autowired
    private DsnProcessService dsnProcessService;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private SheetService sheetService;
    @Autowired
    private DsnGantService dsnGantService;
    @Autowired
    private DsnGantMapper dsnGantMapper;

    @GetMapping("/createPage")
    @RequiresPermissions({"dsn:proTasks:view"})
    public String createPage(Model model) {
        model.addAttribute("deptId", ShiroUtils.getUser().getDeptId());
        model.addAttribute("deptName", ShiroUtils.getUser().getBranchName());
        model.addAttribute("createName", ShiroUtils.getUser().getName());
        return "designtask/add/createGantPage";
    }

    @GetMapping("/project/list")
    @ResponseBody
//    @RequiresPermissions({"dsn:proTasks:view"})
    public List<Project> proList(String q) {
        return sheetService.findProjectByCode(q);
    }

    /**
     * 根据项目编号和登入人员获取项目计划
     *
     * @param proCode 项目编号
     * @return
     */
    @GetMapping("/gantList")
    @ResponseBody
    @RequiresPermissions({"dsn:proTasks:view"})
    public JsonResult gantList(String proCode) {

        return jsonResult(dsnGantService.findGantListByProCodeAndCreateCode(proCode, ShiroUtils.getUser().getAccount()));
    }

    /**
     * 保存项目计划甘特图
     *
     * @param mOrderInsertModel
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"dsn:proTasks:edit"})
    public JsonResult outCreate(@RequestBody DsnGantBillModel dsnGantBillModel) {
        return jsonResult(dsnGantService.save(dsnGantBillModel));
    }

    @GetMapping("/gantPage")
    @RequiresPermissions({"dsn:proTaskinfor:view"})
    public String gantPage(Model model) {

        model.addAttribute("taskTypes", dsnGantMapper.findDsnComTaskName());
        return "designtask/list/gantPage";
    }

    /**
     * 项目任务项查看
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/gantPage")
    @RequiresPermissions({"dsn:proTaskinfor:view"})
    public JsonResult findDsnGantByQuery(DsnGant dsnGant) {
        if (dsnGant.getProCodeQuery() != null && dsnGant.getProCodeQuery() != ""){
            List<Integer> ids = dsnGantMapper.findDeptIdsByDeptId(ShiroUtils.getUser().getDeptId());
            // 没有符合要求的科室带入一个查询不到的条件，否则会查询全部记录
            if (ids.size() > 0) {
                dsnGant.setIds(StringUtils.join(ids.toArray(), ","));
            }
        }
        startPage();
        List<DsnGant> list = dsnGantService.findDsnGantByQuery(dsnGant);
        return jsonResult(list);
    }

    @GetMapping("/gantWorkPage")
    @RequiresPermissions({"dsn:employee:view"})
    public String gantWork() {
        return "designtask/query/gantQuery";
    }

    /**
     * 根据条件查询员工业绩
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/gantWork")
    @RequiresPermissions({"dsn:employee:view"})
    public JsonResult findTaskerWorkByQuery(DsnGant dsnGant) {
        startPage();
        List<DsnGantWorkModel> list = dsnGantService.findTaskerWorkByQuery(dsnGant);
        return jsonResult(list);
    }

    /**
     * 项目任务项查看
     *
     * @param dsnGant
     * @return
     */
    @ResponseBody
    @PostMapping("/gantWorkDetail")
    @RequiresPermissions({"dsn:employee:view"})
    public JsonResult findTaskerWorkDetailByQuery(DsnGant dsnGant) {
        startPage();
        List<DsnGant> list = dsnGantService.findDsnGantByQuery(dsnGant);
        return jsonResult(list);
    }

}
